/* eslint-disable @typescript-eslint/no-var-requires */

const path = require('path');

const publicPath = path.resolve(path.dirname(__filename), 'public');

const CompressionWebpackPlugin = require('compression-webpack-plugin');

module.exports = {
    // assetsDir: 'static',
    devServer: {
        port: 10086,
        proxy: {
            '/prod-api': {
                target: 'http://101.200.182.155:8099',
                // target: 'http://192.168.1.106:8090',
                changeOrigin: true,
                pathRewrite: {
                    '^/prod-api': '/',
                },
            },
        },
    },
    css: {
        loaderOptions: {
            sass: {
                prependData: `@import "~@/styles/styles.scss";`,
            },
        },
    },

    // 打包时进行压缩
    configureWebpack: config => {
        // 开发环境不需要gzip
        if (process.env.NODE_ENV !== 'production') return;
        config.plugins.push(
            new CompressionWebpackPlugin({
                // 正在匹配需要压缩的文件后缀
                test: /\.(js|css|svg|woff|ttf|json|html)$/,
                // 大于10kb的会压缩
                threshold: 10240,
                // 其余配置查看compression-webpack-plugin
            })
        );
    },

    chainWebpack(config) {
        config.resolve.alias.delete('@');

        // 开发环境下分析包环境
        // config
        //     .plugin('webpack-bundle-analyzer')
        //     .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin);

        config.resolve
            .plugin('tsconfig-paths')
            .use(require('tsconfig-paths-webpack-plugin'));
        // Pug Loader
        config.module
            .rule('pug')
            .test(/\.pug$/)
            .use('pug-plain-loader')
            .loader('pug-plain-loader')
            .options({ basedir: publicPath })
            .end();

        config.module
            .rule('pug')
            .test(/\.pug$/)
            .oneOf('pug-vue')
            .use('pug-plain-loader')
            .loader('pug-plain-loader')
            .options({ basedir: publicPath })
            .end();

        config.module
            .rule('pug')
            .test(/\.pug$/)
            .oneOf('pug-template')
            .use('pug-plain-loader')
            .loader('pug-plain-loader')
            .options({ basedir: publicPath })
            .end();
    },
};
